function getUser() {
    const nickEle = document.querySelector('.nick')
    const headerimgEle = document.querySelector('#headerImg')
    //获取数据
    let user = JSON.parse(localStorage.getItem('USER'))
    nickEle.innerHTML = user.nick
    headerimgEle.setAttribute('src', user.headerimg)
}

//退出操作
function logout() {
    const logoutEle = document.querySelector('.logoutBtn')
    logoutEle.onclick = async function () {
        let data = await ajaxPromise({
            method: "GET",
            url: 'http://10.7.171.143:8088/api/logout'
        })
        if (data.resultCode == -1) {
            alert('退出失败')
        } else {
            location.href = '../usermanagerv4.0/login.html'
        }

    }
}

//显示数据列表
async function showUser() {
    tableEle = document.querySelector('table')
    let userObj = await ajaxPromise({
        method: "GET",
        url: 'http://10.7.171.143:8088/api/list'
    })
    if (userObj.resultCode == -1) {
        alert('数据显示失败')
    } else {
        let str = `
                        <tr>
                            <th>序号</th>
                            <th>头像</th>
                            <th>用户名</th>
                            <th>年龄</th>
                            <th>性别</th>
                            <th>公司</th>
                            <th>操作</th>
                        </tr>
                    `
        userObj.resultInfo.forEach(item => {
            if (item.sex == 0) {
                item.sex = '男'
            } else {
                item.sex = '女'
            }
            let newStr = `
                            <tr>
                                <td>${item.uid}</td>
                                <td><img src="${item.headerimg}" width=50px height=50px></td>
                                <td>${item.username}</td>
                                <td>${item.age}</td>
                                <td>${item.sex}</td>
                                <td>${item.company}</td>
                                <td><a href="#" class="delete">操作</a></td>
                            </tr>
                        `
            str += newStr

        })
        tableEle.innerHTML = str
    }
}
//数据操作
function onUser() {
    tableEle = document.querySelector('table')

    tableEle.onclick = async function (e) {
        e = e || window.event
        let target = e.target || e.srcElement
        if (target.getAttribute('class') == 'delete') {
            let id = target.parentElement.parentElement.firstElementChild.innerHTML
            let userObj = await ajaxPromise({
                method: "GET",
                url: 'http://10.7.171.143:8088/api/delete',
                data: {
                    uid: id
                }
            })
            if (userObj.resultCode == -1) {
                alert('删除失败！')
            } else {
                let isDelete = confirm('确定要删除？')
                if (isDelete) {
                    alert('删除成功！')
                }
                showUser()
            }
        }
    }
}

getUser()
logout()
showUser()
onUser()